package demo.缓存;

import common.entity.TUser;
import mybatis.mapper.TJobHistoryMapper;
import mybatis.mapper.TUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class zMybatisCacheTest {

	private SqlSessionFactory sqlSessionFactory;

	@Before
	public void init() throws IOException {

		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		// 1.读取mybatis配置文件创SqlSessionFactory
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		inputStream.close();
	}


	/**
	 * 测试一级缓存
	 * 案例:ZJJ_Mybatis_2019/09/30_ 4:20:05_6m00g
	 * 如果命中了缓存,控制台会打印信息
	 * Cache Hit Ratio
	 */
	@Test
	public void Test1LevelCache() {


		SqlSession session1 = sqlSessionFactory.openSession();
		TUserMapper userMapper1 = session1.getMapper(TUserMapper.class);
		String email = "qq.com";
		Byte sex = 1;
		List<TUser> list1 = userMapper1.selectByEmailAndSex2(email, sex);
		System.out.println(list1.size());


		//增删改操作会清空一级缓存和二级缓存
//		TUser userInsert = new TUser();
//		userInsert.setUserName("test1");
//		userInsert.setRealName("realname1");
//		userInsert.setEmail("myemail1");
//		userMapper1.insert1(userInsert);

		/*直接走缓存,没有查询数据库*/
		List<TUser> list2 = userMapper1.selectByEmailAndSex2(email, sex);
		System.out.println(list2.toString());

		HashMap<String, Object> map = new HashMap<>();
		map.put("email", email);
		map.put("sex", sex);


		/*虽然sql语句和参数是一样的,但是方法名不一样
		 * 不会走缓存,直接走数据库
		 * */
		List<TUser> list3 = userMapper1.selectByEmailAndSex1(map);
		System.out.println(list3.toString());


		session1.close();


		SqlSession session2 = sqlSessionFactory.openSession();
		TUserMapper userMapper2 = session2.getMapper(TUserMapper.class);
		/*还是会走数据库,因为是新的sqlSession了,如果发现走了缓存,那说明已经开启二级缓存了
		.*/
		List<TUser> list4 = userMapper2.selectByEmailAndSex2(email, sex);
		System.out.println(list2.toString());
		session1.close();


	}

	/**
	 * 测试二级缓存
	 * 案例:ZJJ_Mybatis_2019/09/30_ 4:30:59_hkdk5
	 */
	@Test
	public void Test2LevelCache() {
		SqlSession session1 = sqlSessionFactory.openSession();
		TUserMapper userMapper1 = session1.getMapper(TUserMapper.class);
		String email = "qq.com";
		Byte sex = 1;
		List<TUser> list1 = userMapper1.selectByEmailAndSex2(email, sex);
		System.out.println(list1.size());


//		TUser userInsert = new TUser();
//		userInsert.setUserName("test1");
//		userInsert.setRealName("realname1");
//		userInsert.setEmail("myemail1");
//		userMapper1.insert1(userInsert);

		List<TUser> list2 = userMapper1.selectByEmailAndSex2(email, sex);
		System.out.println(list2.toString());


		session1.close();


		SqlSession session2 = sqlSessionFactory.openSession();
		TUserMapper userMapper2 = session2.getMapper(TUserMapper.class);
		List<TUser> list3 = userMapper2.selectByEmailAndSex2(email, sex);
		System.out.println(list3.toString());
		session2.close();

		//虽然同样的sql语句、入参，但是不在同一个namespace下，不走二级缓存,直接走数据库的查询
		SqlSession session3 = sqlSessionFactory.openSession();
		TJobHistoryMapper userMapper3 = session3.getMapper(TJobHistoryMapper.class);
		List<TUser> list4 = userMapper3.selectByEmailAndSex2(email, sex);
		System.out.println(list4.toString());
		session3.close();


	}


}
